/*
 * Copyright (c) 2009 Marius Mathiesen <marius.mathiesen@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
 */
package no.trafikanten;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Date;

public class DepartureGroup 
{
	private String name, transportationName;
	private ArrayList<Date>departures;
	private int lineNumber, direction, transportationId;
	public DepartureGroup(int direction, int lineNumber, String name)
	{
		this.direction = direction;
		this.lineNumber = lineNumber;
		this.name = name;
		this.departures = new ArrayList<Date>();
	}
	
	public String getTransportationName()
	{
		switch(this.transportationId) {
			case 0: return "trikk"; // tram
			case 1: return "buss"; // bus
			case 2: return "T-bane"; // subway
			case 3: return "båt"; // boat
			default: return this.transportationName;
		}
	}
	
	// Bus, icon etc. - int
	public void setTransportationId(int id)
	{
		transportationId = id;
	}
	
	public int getTransportationId()
	{
		return this.transportationId;
	}
	
	public void setTransportationName(String name)
	{
		transportationName = name;
	}
	
	public void addDepartures(Date t)
	{
		this.departures.add(t);
	}
	
	public String getName()
	{
		return this.name;
	}
	public String getDepartureString(Date now, boolean estimate)
	{
		Collections.sort(departures);
		StringBuffer buff = new StringBuffer();
		Iterator<Date> it = departures.iterator();
		while(it.hasNext())
		{
			Date departureTime = it.next();
//			String nS = now.toString();
//			String dS = departureTime.toString();
			if (estimate) buff.append("ca. ");
			String dep = Departure.getDateString(departureTime, now);
			buff.append(dep + "   ");
		}
		return buff.toString().trim();
	}
	/**
	 * Used for ArrayList.contains - to keep only one group per departure
	 */
	public boolean equals(Object _g)
	{
		DepartureGroup g = (DepartureGroup) _g;
		return ((g.getLineNumber() == lineNumber) && (g.getDirection() == direction));
	}
	
	public int getLineNumber()
	{
		return this.lineNumber;
	}
	public void setDirection(int d)
	{
		direction = d;
	}
	public int getDirection()
	{
		return this.direction;
	}
}
